package com.tjec.project.businessWorkbench.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.tjec.common.constant.AttachmentConstants;
import com.tjec.common.constant.Constants;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsMdExpertType;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsMdExpertTypeService;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.tjec.project.businessWorkbench.mapper.BmsBusinessExpertUseMapper;
import com.tjec.project.businessWorkbench.domain.BmsBusinessExpertUse;
import com.tjec.project.businessWorkbench.dto.BmsExpertUserDto;
import com.tjec.project.businessWorkbench.service.IBmsBusinessExpertUseService;
import com.tjec.project.businessWorkbench.vo.AutoExtractVo;
import com.tjec.project.businessWorkbench.vo.ExpertAutoExtractVo;
import org.springframework.util.CollectionUtils;

/**
 * 专家使用Service业务层处理
 * 
 * @author tjec
 * @date 2020-04-22
 */
@Service
public class BmsBusinessExpertUseServiceImpl implements IBmsBusinessExpertUseService 
{
    @Autowired
    private BmsBusinessExpertUseMapper bmsBusinessExpertUseMapper;

    @Autowired
    private BmsAttachmentMapper bmsAttachmentMapper;

    @Autowired
    private TokenService tokenService;

	@Autowired
	private IBmsMdExpertTypeService bmsMdExpertTypeService;
    
    @Value("${sftp.http}")
	private String http;
	@Value("${sftp.ip}")
	private String ip;

    /**
     * 查询专家使用
     * 
     * @param id 专家使用ID
     * @return 专家使用
     */
    @Override
    public BmsBusinessExpertUse selectBmsBusinessExpertUseByGuid(String guid)
    {
        return bmsBusinessExpertUseMapper.selectBmsBusinessExpertUseByGuid(guid);
    }

    /**
     * 查询专家使用列表
     * 
     * @param bmsBusinessExpertUse 专家使用
     * @return 专家使用
     */
    @Override
    public List<BmsBusinessExpertUse> selectBmsBusinessExpertUseList(BmsBusinessExpertUse bmsBusinessExpertUse)
    {
    	List<BmsBusinessExpertUse> list = bmsBusinessExpertUseMapper.selectBmsBusinessExpertUseList(bmsBusinessExpertUse);
    	if(list!=null && list.size()>0) {
    		list.forEach(item->{
				if(StringUtils.isNotBlank(item.getProfessionCategory())){
					List<BmsMdExpertType> professionCategoryExpertType = bmsMdExpertTypeService.selectBmsMdExpertTypeByGuids(item.getProfessionCategory().split(";"));
					String professionCategoryNames = "";
					for (BmsMdExpertType bmsMdExpertType : professionCategoryExpertType) {
						professionCategoryNames += bmsMdExpertType.getCname()+";";
					}
					item.setProfessionCategoryName(professionCategoryNames);
				}
				if(StringUtils.isNotBlank(item.getProfessionCategory())){
					item.setProfessionCategorys( item.getProfessionCategory().split(";"));
				}
				if(StringUtils.isNotBlank(item.getBusinessMerit())){
					item.setBusinessMerits( item.getBusinessMerit().split(";"));
				}
				if(StringUtils.isNotBlank(item.getIndustryMerit())){
					item.setIndustryMerits( item.getIndustryMerit().split(";"));
				}
    			if(item.getAttachments()!=null && item.getAttachments().size()>0) {
    				for(int i=0;i<item.getAttachments().size();i++) {
    					if(!"".equals(item.getAttachments().get(i).getStoragePath()) && item.getAttachments().get(i).getStoragePath()!=null) {
    						item.getAttachments().get(i).setStoragePath(http + ip +Constants.RESOURCE_PREFIX + item.getAttachments().get(i).getStoragePath());
    					}
    				}
    			}
    		});
    	}
        return list;
    }

    /**
     * 新增专家使用
     * 
     * @param bmsBusinessExpertUse 专家使用
     * @return 结果
     */
    @Override
    public int insertBmsBusinessExpertUse(List<BmsBusinessExpertUse> bmsBusinessExpertUses)
    {
       /* for (BmsBusinessExpertUse expertUse : bmsBusinessExpertUses) {
            addAttachment(expertUse);
        }*/
    	//bmsBusinessExpertUses.setCreateTime(DateUtils.getNowDate());
        return bmsBusinessExpertUseMapper.insertBmsBusinessExpertUse(bmsBusinessExpertUses);
    }

    private void addAttachment(BmsBusinessExpertUse bmsBusinessExpertUse){
        //更新专家持证情况、上传附件，每次删除原来的，添加最新的
        if(StringUtils.isNotBlank(bmsBusinessExpertUse.getGuid())){
            bmsAttachmentMapper.updateBmsAttachmentByBizId(new String[]{bmsBusinessExpertUse.getGuid()}, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        }
        if(!CollectionUtils.isEmpty(bmsBusinessExpertUse.getUseAttachmentIds())){
            bmsAttachmentMapper.updateBmsAttachmentByGuidsUpdateBiz(bmsBusinessExpertUse.getUseAttachmentIds(), bmsBusinessExpertUse.getGuid(), AttachmentConstants.EXPERT_PERSON, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        }
    }

    /**
     * 修改专家使用
     * 
     * @param bmsBusinessExpertUse 专家使用
     * @return 结果
     */
    @Override
    public int updateBmsBusinessExpertUse(BmsBusinessExpertUse bmsBusinessExpertUse)
    {
        addAttachment(bmsBusinessExpertUse);
        bmsBusinessExpertUse.setUpdateTime(DateUtils.getNowDate());
        return bmsBusinessExpertUseMapper.updateBmsBusinessExpertUse(bmsBusinessExpertUse);
    }

    /**
     * 批量删除专家使用
     * 
     * @param guids 需要删除的专家使用ID
     * @return 结果
     */
    @Override
    public int deleteBmsBusinessExpertUseByGuids(String[] guids)
    {
        return bmsBusinessExpertUseMapper.deleteBmsBusinessExpertUseByGuids(guids);
    }

    /**
     * 删除专家使用信息
     * 
     * @param id 专家使用ID
     * @return 结果
     */
    @Override
    public int deleteBmsBusinessExpertUseByGuid(String guid)
    {
        return bmsBusinessExpertUseMapper.deleteBmsBusinessExpertUseByGuid(guid);
    }

    /**
      * 自动抽取
     * 
     * @param expertAutoExtractVos 抽取条件
     * @return 结果
     */
	@Override
	public int autoExtract(ExpertAutoExtractVo expertAutoExtractVo) {
		if("0".equals(expertAutoExtractVo.getSource())) {//个人专家库
			List<BmsBusinessExpertUse> lists = new ArrayList<BmsBusinessExpertUse>();
			for(AutoExtractVo autoExtractVo : expertAutoExtractVo.getAutoExtractVo()) {
				autoExtractVo.setBusinessId(expertAutoExtractVo.getBusinessId());
				List<String> professionCategorys = bmsMdExpertTypeService.selectBmsMdExpertTypeChildrenByGuid(autoExtractVo.getProfessionCategory());
				autoExtractVo.setProfessionCategorys(professionCategorys);
				List<BmsBusinessExpertUse> bmsBusinessExpertUses= bmsBusinessExpertUseMapper.autoExtractPerson(autoExtractVo);
				if(bmsBusinessExpertUses != null && bmsBusinessExpertUses.size()>0) {
					lists.addAll(bmsBusinessExpertUses);
				}
			}
			int res = 0;
			if(lists.size()>0) {
				lists.forEach(item ->{
					item.setBusinessId(expertAutoExtractVo.getBusinessId());
				});
				res = bmsBusinessExpertUseMapper.insertBmsBusinessExpertUse(lists);
			}
			return res;
		}else {//企业专家库
			List<BmsBusinessExpertUse> lists = new ArrayList<BmsBusinessExpertUse>();
			for(AutoExtractVo autoExtractVo : expertAutoExtractVo.getAutoExtractVo()) {
				List<String> professionCategorys = bmsMdExpertTypeService.selectBmsMdExpertTypeChildrenByGuid(autoExtractVo.getProfessionCategory());
				autoExtractVo.setProfessionCategorys(professionCategorys);
				List<BmsBusinessExpertUse> bmsBusinessExpertUses= bmsBusinessExpertUseMapper.autoExtractCompany(autoExtractVo);
				if(bmsBusinessExpertUses != null && bmsBusinessExpertUses.size()>0) {
					lists.addAll(bmsBusinessExpertUses);
				}
			}
			
			int res = 0;
			if(lists.size()>0) {
				lists.forEach(item ->{
					item.setBusinessId(expertAutoExtractVo.getBusinessId());
				});
				res = bmsBusinessExpertUseMapper.insertBmsBusinessExpertUse(lists);
			}
			return res;
		}
	}

	@Override
	public BmsExpertUserDto getRecommendCounts(String expertId, String businessId, String flag) {
		return bmsBusinessExpertUseMapper.getRecommendCounts(expertId, businessId, flag);
	}

	@Override
	public String selectIsUseCompanyExpert(String businessId) {
		return bmsBusinessExpertUseMapper.selectIsUseCompanyExpert(businessId);
	}
}
